import 'package:auto_size_text/auto_size_text.dart';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../dd_core/dd_config.dart';
import '../../../dd_core/dd_log_service.dart';
import '../../../dd_core/dd_other.dart';
import '../../../dd_models/dd_hive_works_model.dart';
import '../../../r.dart';
import '../../dd_personal_center/views/dd_view_background.dart';
import '../controllers/dd_user_space_controller.dart';

class DDUserSpacePage extends StatefulWidget {
  final HiveWorksModel userModel;

  const DDUserSpacePage({Key? key, required this.userModel}) : super(key: key);

  @override
  State<DDUserSpacePage> createState() => _DDUserSpacePageState();
}

class _DDUserSpacePageState extends State<DDUserSpacePage> {
  @override
  Widget build(BuildContext context) {
    return GetBuilder<DDUserSpaceController>(
        init: DDUserSpaceController(widget.userModel),
        initState: (state) {
          DDLogService().ddGa(page: "user_space_page");
        },
        dispose: (state) {},
        builder: (controller) {
          return Scaffold(
            body: Obx(
              () => Column(
                children: [
                  Stack(
                    children: [
                      Column(
                        children: [
                          Padding(
                            padding:
                                EdgeInsets.only(top: 0.0, left: 0, right: 0),
                            child: Image.asset(
                              R.assetsImagesPerson1,
                              width: double.infinity,
                              fit: BoxFit.fill,
                            ),
                          ),
                          SizedBox(
                            height: 44,
                          )
                        ],
                      ),
                      Positioned(
                        top: 50,
                        left: 16,
                        child: IconButton(
                          icon: Image.asset(R.assetsImagesHomeSecond1),
                          onPressed: () {
                            Get.back();
                          },
                        ),
                      ),
                      Positioned(
                        top: 50,
                        right: 16,
                        child: Row(
                          children: [
                            IconButton(
                              icon: controller.userModelObx.value.myFollow ??
                                      false
                                  ? Image.asset(R.assetsImagesHome7)
                                  : Image.asset(R.assetsImagesHome15),
                              onPressed: () {
                                controller.updateFollow();
                              },
                            ),
                            IconButton(
                              icon: Image.asset(
                                R.assetsImagesHomeSecond2,
                                height: 36,
                                width: 36,
                                fit: BoxFit.cover,
                              ),
                              onPressed: () {
                                _showBottomSheet(context, controller);
                              },
                            ),
                          ],
                        ),
                      ),
                      Positioned(
                        bottom: 0,
                        left: MediaQuery.of(context).size.width / 2 - 50,
                        child: DDViewBackGround(
                          content: CachedNetworkImage(
                            width: 88,
                            height: 88,
                            imageUrl:
                                controller.userModelObx.value.avatarUrl ?? '',
                            placeholder: (context, url) => Placeholder(),
                            errorWidget: (context, url, error) => Placeholder(),
                          ),
                        ),
                      ),
                    ],
                  ),
                  SizedBox(height: 16),
                  AutoSizeText(
                    controller.userModelObx.value.nickname ?? '',
                    maxLines: 1,
                    minFontSize: 8,
                    style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
                  ),
                  SizedBox(height: 16),
                  Expanded(
                    child: GridView.builder(
                      padding: EdgeInsets.all(16),
                      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                        crossAxisCount: 2,
                        crossAxisSpacing: 8,
                        mainAxisSpacing: 20,
                        childAspectRatio: 0.75,
                      ),
                      itemCount: controller.userList
                          .length, // Replace with actual number of posts
                      itemBuilder: (context, index) {
                        HiveWorksModel model = controller.userList[index];
                        return DDViewBackGround(
                          content: Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              Padding(
                                padding: EdgeInsets.only(
                                    left: 4.0, right: 4.0, top: 4.0, bottom: 0),
                                child: AspectRatio(
                                  aspectRatio: 1.05,
                                  child: ClipRRect(
                                    borderRadius: BorderRadius.vertical(
                                      top: Radius.circular(4),
                                    ),
                                    child: model.coverImage != null
                                        ? Image.asset(
                                            model.coverImage?[0] ?? '',
                                            height: 150,
                                            width: double.infinity,
                                            fit: BoxFit.cover,
                                          )
                                        : Image(
                                            image: Image.memory(
                                                    model.allImageData![0])
                                                .image,
                                            fit: BoxFit.cover,
                                            height: 150,
                                            width: double.infinity,
                                          ),
                                  ),
                                ),
                              ),
                              Padding(
                                padding: EdgeInsets.only(
                                    left: 8.0, right: 8.0, top: 8, bottom: 0),
                                child: Text(
                                  model.description ?? '',
                                  style: TextStyle(fontSize: 12),
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ),
                              Padding(
                                padding:
                                    const EdgeInsets.symmetric(horizontal: 8.0),
                                child: Row(
                                  children: [
                                    CircleAvatar(
                                      radius: 15,
                                      backgroundImage: NetworkImage(
                                        model.avatarUrl ?? '',
                                      ),
                                    ),
                                    SizedBox(width: 8),
                                    Expanded(
                                      child: Text(
                                        model.nickname ?? '',
                                        maxLines: 1,
                                        style: TextStyle(fontSize: 14),
                                        overflow: TextOverflow.ellipsis,
                                      ),
                                    ),
                                    Container(
                                      width: 40,
                                      height: 40,
                                      child: IconButton(
                                        icon: Image.asset(
                                            (model.myLikes ?? false)
                                                ? R.assetsImagesHomeSecond6
                                                : R.assetsImagesHomeSecond4),
                                        onPressed: () {
                                          controller.updateMyLike(model);
                                        },
                                      ),
                                    ),
                                  ],
                                ),
                              ),
                            ],
                          ),
                        );
                      },
                    ),
                  ),
                ],
              ),
            ),
          );
        });
  }

  void _showBottomSheet(
      BuildContext context, DDUserSpaceController _controller) {
    showModalBottomSheet(
      context: context,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16.0)),
      ),
      builder: (BuildContext context) {
        return SafeArea(
            child: Container(
          padding: EdgeInsets.all(16.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              ListTile(
                title: Center(
                    child: Text(
                        _controller.userModelObx.value.myFollow ?? false
                            ? 'Unfollow'
                            : 'Follow',
                        style: TextStyle(fontSize: 16))),
                onTap: () {
                  Navigator.pop(context);
                  _controller.updateFollow();
                },
              ),
              ListTile(
                title: Center(
                    child: Text(
                        _controller.userModelObx.value.myBlock ?? false
                            ? 'Unblock'
                            : 'Block',
                        style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your block action here
                  Navigator.pop(context);
                  _controller.updateBlock();
                },
              ),
              ListTile(
                title: Center(
                    child: Text('Report', style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your report action here
                  Navigator.pop(context);
                  _showBottomSheet1(context, _controller);
                },
              ),
              SizedBox(height: 16),
              DDViewBackGround(
                content: Container(
                  width: 183,
                  height: 44,
                  child: DDSafeButton(
                    // padding: EdgeInsets.only(top: 20, bottom: 20),
                    child: Text(
                      "Cancel",
                      style: TextStyle(fontSize: 16, color: Color(0xff53BFD7)),
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ),
              ),
            ],
          ),
        ));
      },
    );
  }

  void _showBottomSheet1(
      BuildContext context, DDUserSpaceController controller) {
    showModalBottomSheet(
      context: context,
      shape: RoundedRectangleBorder(
        borderRadius: BorderRadius.vertical(top: Radius.circular(16.0)),
      ),
      builder: (BuildContext context) {
        return SafeArea(
            child: Container(
          padding: EdgeInsets.all(16.0),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              ListTile(
                title: Center(
                    child:
                        Text('Pornographic', style: TextStyle(fontSize: 16))),
                onTap: () {
                  Navigator.pop(context);
                  //_showSnackBar("Report Successfully", context);
                  controller.report('Pornographic');
                },
              ),
              ListTile(
                title: Center(
                    child:
                        Text('False gender', style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your block action here
                  Navigator.pop(context);
                  controller.report('False gender');
                },
              ),
              ListTile(
                title: Center(
                    child: Text('Fraud', style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your report action here
                  Navigator.pop(context);
                  controller.report('Fraud');
                },
              ),
              ListTile(
                title: Center(
                    child: Text('Political sensitive',
                        style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your report action here
                  Navigator.pop(context);
                  controller.report('Political sensitive');
                },
              ),
              ListTile(
                title: Center(
                    child: Text('Other', style: TextStyle(fontSize: 16))),
                onTap: () {
                  // Add your report action here
                  Navigator.pop(context);
                  controller.report('Other');
                },
              ),
              SizedBox(height: 16),
              DDViewBackGround(
                content: Container(
                  width: 183,
                  height: 44,
                  child: DDSafeButton(
                    // padding: EdgeInsets.only(top: 20, bottom: 20),
                    child: Text(
                      "Cancel",
                      style: TextStyle(fontSize: 16, color: Color(0xff53BFD7)),
                    ),
                    onPressed: () {
                      Navigator.pop(context);
                    },
                  ),
                ),
              ),

              // TextButton(
              //   onPressed: () {
              //     Navigator.pop(context);
              //   },
              //   child: Text('Cancel', style: TextStyle(fontSize: 18, color: Colors.blue)),
              // ),
            ],
          ),
        ));
      },
    );
  }

  void _showSnackBar(String message, BuildContext context) {
    ScaffoldMessenger.of(context).showSnackBar(
      SnackBar(
        content: Text(message),
      ),
    );
  }
}
